package controller.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.ResourceType;
import model.User;

import db.DAOFactory;

import tools.MD5_Encoding;

/**
 * Description: 对管理员进行资源类型的管理请求进行处理。
 * 
 * @since  2009/3/2
 * @author ashen
 *  
 */
public class ResourceTypeDo extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ResourceTypeDo() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doDelete method of the servlet. <br>
	 *
	 * This method is called when a HTTP delete request is received.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String msg="";
		
		//获取请求要获取的用户信息的ID，根据ID来返回信息
		String uri=request.getRequestURI();
		String[] uriStr=uri.split("/");
        int resourceTypeId=Integer.parseInt(uriStr[uriStr.length-1]);
        if(resourceTypeId!=0){
	        if(DAOFactory.getResourceTypeDAO().delete(resourceTypeId)){
	        	msg="删除成功";
	        }else
	        	msg="删除失败";
        }else
        	msg="删除失败,没有此项资源。";
		
		out.println("<p>"+msg+"</p>");
		out.flush();
		out.close();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//输出资源类型列表
		tools.OutputXml.output(new model.dto.TypeList(), response);
				
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String msg="";
		
		ResourceType resourceType =new ResourceType();
		String type=request.getParameter("type");
		if(type!=null){
			resourceType.setType(type);
			//验证资源类型名称是否被使用，资源类型名称不能重复
			ResourceType temp=DAOFactory.getResourceTypeDAO().findByType(resourceType.getType());
			if(temp==null){
				resourceType.setDescription(request.getParameter("description"));
				//保存用户到数据库
				if(DAOFactory.getResourceTypeDAO().save(resourceType)){
					msg="增加资源类型成功";
				}else
					msg="增加资源类型失败";
			}else
				msg="增加资源类型失败，资源类型已被占用";
		}else
			msg="增加资源类型失败，没有填写资源类型";
		
		out.println("<p>"+msg+"</p>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
